package bigbrother.modules.help;

import bigbrother.command.AbstractCommand;
import bigbrother.command.Command;
import bigbrother.core.IrcBotCore;
import bigbrother.modules.AbstractModule;
import bigbrother.modules.Module;
import org.apache.wicket.Component;
import org.apache.wicket.markup.html.basic.MultiLineLabel;

/**
 * A module that provides a help command that lists all available commands, and their documentation.
 *
 * @author Hans Haggstrom
 */
// TODO: As the number of modules grow, the help text will become too long.  Split into two levels, help just lists the modules, and help module lists the command helps
// TODO: Some way to get all help texts as one list, maybe pre-formatted for use on an html page.    
public final class HelpModule
        extends AbstractModule
{

    //======================================================================
    // Public Constants

    public static final String HELP_MODULE_NAME = "help";

    //======================================================================
    // Public Methods

    //----------------------------------------------------------------------
    // Constructors

    public HelpModule()
    {
        super( HELP_MODULE_NAME );

        registerCommand( new AbstractCommand( "", "Prints the available commands and their descriptions." )
        {

            public String doCommand( final String channel,
                                     final String sender,
                                     final String login,
                                     final String hostname,
                                     final String parameters )
            {
                return generateHelpText();
            }

        } );
    }

    //----------------------------------------------------------------------
    // Module Implementation


    public synchronized Component createWebComponent( final String id )
    {
        return new MultiLineLabel( id, generateHelpText() );
    }

    //======================================================================
    // Protected Methods

    protected void onModuleStarted()
    {

    }


    protected void onModuleStopped()
    {

    }

    //======================================================================
    // Private Methods

    private String generateHelpText()
    {
        final IrcBotCore core = getContext().getCore();

        final StringBuilder doc = new StringBuilder();

        doc.append( "Help for " );
        doc.append( core.getBotModelAndVersion() );
        doc.append( "\n\nAvailable Commands:\n" );

        for ( Module module : core.getActiveModules() )
        {
            doc.append( " \n" );

            for ( Command command : module.getCommands() )
            {
                doc.append( core.getDefaultCommandPrefix() );
                doc.append( module.getIdentifier() );
                final String commandName = command.getCommandName();
                if ( !commandName.equals( "" ) )
                {
                    doc.append( "." );
                    doc.append( commandName );
                }
                doc.append( "  -  " );

                doc.append( command.getDocumentation() );
                doc.append( "\n" );
            }
        }

        doc.append( "\n" );

        return doc.toString();
    }

}
